package tools.shapes;

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Rectangle2D;

import app.DrawingCanvas;

/**
 * This class contains the necessary methods and parameters for drawing and storing text.
 *
 */

public class TextShape extends Shape {

	private Point pos = new Point();
	private StringBuffer str = new StringBuffer();
	
	public void setPos(Point p) {
		pos.setLocation(p);
	}
	
	public void addChar(char c) {
		str.append(c);
	}
	
	public void updateBounds(DrawingCanvas c) {
		Rectangle2D bounds = c.getimageBufferGraphics().getFontMetrics().
			getStringBounds(str.toString(), c.getimageBufferGraphics());
		xMin = pos.x + (int) bounds.getMinX();
		xMax = pos.x + (int) bounds.getMaxX();
		yMin = pos.y + (int) bounds.getMinY();
		yMax = pos.y + (int) bounds.getMaxY();
	}
	
	public void draw(DrawingCanvas c) {
		Color savedColor = c.getpenColor();
		c.getimageBufferGraphics().setColor(this.color);
		c.getimageBufferGraphics().drawString(str.toString(), pos.x, pos.y);
		c.setpenColor(savedColor);
	}

	public void drawOutline(DrawingCanvas c) {
		draw(c);
	}

	public void resize(Point startPos, Point newPos) {
		// This does nothing. We do not support resizing text.
	}

	public void move(Point startPos, Point newPos) {
		int deltaX = newPos.x - startPos.x;
		int deltaY = newPos.y - startPos.y;

		xMin += deltaX;
		xMax += deltaX;
		yMin += deltaY;
		yMax += deltaY;
		pos.setLocation(pos.x + deltaX, pos.y + deltaY);
	}

}
